home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3971 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  131 lines

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem...Problem!
  5. Date: 1 Feb 96 00:29:13 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.823134553@hubcap>
  8. References: <4el09q$6im@ratree.psu.ac.th>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10.  
  11. s3610165@maliwan.psu.ac.th (Sanon CHAOCHAIYAPORN) writes:
  12.  
  13. >Dear all
  14. >        This is a iteration method program. It use to find three values, 
  15. >a b and c. This program will finish when variables, t[], are less than or 
  16. >equal an error value, ERR. But my program has not been runing. Please, 
  17. >advice me...:~( Thanks for your attention.
  18.  
  19. >Source:
  20. >#include<stdio.h>
  21. >#include<conio.h>
  22.  
  23. >#define NUM 8
  24. >#define ERR 1e-4
  25.  
  26. >float v[NUM];
  27. >int i;
  28.  
  29. No need for this declaration of i.  It is initialized to zero and used
  30. only in the function display, where it is never set.  It's a good example
  31. of why global variables are a good thing to avoid (in general).
  32.  
  33. >main()
  34. >{
  35. >  float e[NUM],t[NUM];
  36. >  int i;
  37. >  char pass;
  38.  
  39. The variable pass is never used.
  40.  
  41. >  clrscr();
  42. >  for(i=0;i<NUM;i++)
  43. >    v[i] = e[i] = t[i] = 0;
  44. >  do
  45. >  {
  46. >    i = 0;
  47.  
  48. In fact, why not just substitute out the i from the following expressions:
  49.  
  50. >    v[i]   = (v[i+1] + v[i+2]) / 8;
  51. >    v[i+1] = (v[i] + v[i+3] + 40) / 8;
  52. >    v[i+2] = (v[i] + v[i+3] + v[i+5] + 50) / 8;
  53. >    v[i+3] = (v[i+1] + v[i+2] + 110) / 8;
  54. >    v[i+4] = (v[i+5] + v[i+6] + 150) / 8;
  55. >    v[i+5] = (v[i+2] + v[i+4] + v[i+7] + 70) / 8;
  56. >    v[i+6] = (v[i+4] + v[i+7] + 200) / 8;
  57. >    v[i+7] = (v[i+5] + v[i+6] + 200) / 8;
  58.  
  59.     v[0] = (v[1] + v[2]) / 8;
  60.     v[1] = (v[0] + v[3] + 40) / 8;
  61.     v[2] = (v[0] + v[3] + v[5] + 50) / 8;
  62.     v[3] = (v[1] + v[2] + 110) / 8;
  63.     v[4] = (v[5] + v[6] + 150) / 8;
  64.     v[5] = (v[2] + v[4] + v[7] + 70) / 8;
  65.     v[6] = (v[4] + v[7] + 200) / 8;
  66.     v[7] = (v[5] + v[6] + 200) / 8;
  67.  
  68. Are the constants derived from some formula?  Is the 8 from NUM?  Is
  69. there some formula that tells what each line should be as a function
  70. of NUM and the element of v[] that is being computed?  If so, you
  71. could do this calculation in a loop, and make it independent of NUM.
  72.  
  73. >    display();
  74. >    for(i=0;i<NUM;i++)
  75. >      t[i] = v[i] - e[i];
  76. >    for(i=0;i<NUM;i++)
  77. >     e[i] = v[i];
  78. >  }while((t[0] <= ERR) && (t[1] <= ERR) && (t[2] <= ERR) && (t[3] <= ERR) &&
  79. >      (t[4] <= ERR) && (t[5] <= ERR) && (t[6] <= ERR) && (t[7] <= ERR));
  80.  
  81. This loop is executed only as long as all values of t[] are less than
  82. ERR.  Since this is not the case the first time through the loop, the
  83. loop exits after the first pass.  You probably want the negation of
  84. this condition.  Also, you could test the condition in a loop, to
  85. make it independent of NUM.
  86.  
  87. >  printf("\na is %8.4f volt",v[1]);
  88. >  printf("\nb is %8.4f volt",v[2]);
  89. >  printf("\nc is %8.4f volt",v[4]);
  90.  
  91. Add
  92.   printf("\n");
  93.  
  94.   return 0;
  95. >}
  96.  
  97. >display()
  98. >{
  99. >  printf("%10.4f",v[i]);
  100. >  printf("%10.4f",v[i+1]);
  101. >  printf("%10.4f",v[i+2]);
  102. >  printf("%10.4f",v[i+3]);
  103. >  printf("%10.4f",v[i+4]);
  104. >  printf("%10.4f",v[i+5]);
  105. >  printf("%10.4f",v[i+6]);
  106. >  printf("%10.4f",v[i+7]);
  107. >}
  108.  
  109. The i in this routine is that global i, initialized to zero.  Why not
  110. substitute it out?  Better yet, why not do this in a loop?  Also,
  111. since display does not return a value, you should declare it as
  112. returning void (although maybe that's advanced for you at this point).
  113.  
  114. void display()
  115. {
  116.   int i;
  117.  
  118.   for ( i = 0;  i < NUM;  i++ )
  119.     printf("%10.4f ", v[i]);
  120.  
  121.   printf("\n");
  122. }
  123.  
  124. If I were writing this, I'd pass v[] (and NUM) as a parameter to
  125. display(), but maybe that's advanced for you also.
  126.  
  127. -- 
  128.         Matthew Saltzman
  129.         Clemson University Math Sciences
  130.         mjs@clemson.edu
  131.